package com.sanyuan.cloud.module.system.service.nongmo.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanyuan.cloud.framework.common.pojo.PageResult;
import com.sanyuan.cloud.framework.common.util.id.SnowflakeIdUtil;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.req.PositionAssignReq;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.req.PositionListReq;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.vo.PositionListVO;
import com.sanyuan.cloud.module.system.dal.dataobject.nongmo.SystemPositionDO;
import com.sanyuan.cloud.module.system.dal.dataobject.nongmo.SystemPositionUserDO;
import com.sanyuan.cloud.module.system.dal.dataobject.user.AdminUserDO;
import com.sanyuan.cloud.module.system.dal.mysql.nongmo.SystemPositionMapper;
import com.sanyuan.cloud.module.system.dal.mysql.nongmo.SystemPositionUserMapper;
import com.sanyuan.cloud.module.system.dal.mysql.user.AdminUserMapper;
import com.sanyuan.cloud.module.system.service.nongmo.SystemPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.sanyuan.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sanyuan.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.sanyuan.cloud.module.infra.enums.ErrorCodeConstants.NONGMO_DATA_NOT_EXISTS;
import static com.sanyuan.cloud.module.system.enums.nongmo.CommonEnums.VISIT_STATUS_0;

/**
 * @Description: 标点管理接口实现类
 * @Author: wuyuwei
 * @Date: 2025/3/20
 */
@Service
@Slf4j
public class SystemPositionServiceImpl implements SystemPositionService {

    public static final SnowflakeIdUtil SNOWFLAKE_ID_UTIL = new SnowflakeIdUtil();

    @Resource
    private SystemPositionMapper systemPositionMapper;
    @Resource
    private SystemPositionUserMapper systemPositionUserMapper;
    @Resource
    private AdminUserMapper adminUserMapper;

    @Override
    public PageResult<PositionListVO> getPositionListByPage(PositionListReq req) {
        IPage<SystemPositionDO> pag = new Page<>(req.getPageNo(),req.getPageSize(),true);
        IPage<PositionListVO> pageResult = systemPositionMapper.selectPositionUserByPage(pag, req);
        return new PageResult<>(pageResult.getRecords(),pageResult.getTotal());
    }

    @Override
    public Boolean deleteById(Long id) {
        systemPositionMapper.updateById(id, String.valueOf(getLoginUserId()));
        return true;
    }

    @Override
    public void assign(PositionAssignReq req) {
        SystemPositionDO systemPositionDO = systemPositionMapper.selectById(req.getId());
        if (null == systemPositionDO){
            throw exception(NONGMO_DATA_NOT_EXISTS);
        }
        AdminUserDO adminUserDO = adminUserMapper.selectById(req.getUserId());
        SystemPositionUserDO systemPositionUserDO = systemPositionUserMapper.selectPositionUserByPositionId(systemPositionDO.getId());
        if (null != systemPositionUserDO){
            systemPositionUserMapper.deleteById(systemPositionUserDO.getId());
        }
        SystemPositionUserDO entity = new SystemPositionUserDO();
        entity.setPositionId(req.getId());
        entity.setUserId(req.getUserId());
        if (null != adminUserDO){
            entity.setUsername(adminUserDO.getUsername());
        }
        entity.setStatus(Integer.parseInt(VISIT_STATUS_0.getCode()));
        systemPositionUserMapper.insert(entity);
    }
}
